package game;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.Random;

//O joão é fanta uva e nem ofereceu fgsdsdfgsdf


public class Tela01 extends CanvasAb{
	float gravidade = 500f;
	float VelX =0;
	float VelY = 0;
	float x = 50;
	float y = 260;
	float vel = 200;
	boolean one = true;
	boolean newPlat = false;
	float MAX_FALL_SPEED = 500;
	public static BufferedImage bquadrado = null;
	BufferedImage fundo = null;
	public static BufferedImage tilePlat = null;
	public static ArrayList<Plataforma> platList = new ArrayList<Plataforma>();
	public static boolean JUMP, DOWN, LEFT, RIGHT ;
	Jogador jogador;
	Inputs ip;
	Random rand = new Random();
	public static Plataforma plat;
	public static Quadrado quad;
	public Tela01(){
		
		bquadrado = GamePanel.instance.carregaImagem("Quadrado.png");
		fundo = GamePanel.instance.carregaImagem("Fundo.jpg");
		tilePlat = GamePanel.instance.carregaImagem("tilePlat.png");
		
		jogador = new Jogador(bquadrado, 150, 480, 20, 20);
		plat = new Plataforma(null, 0, 520, 800, 50, 50);
		plat.willFall = false;
		platList.add(plat);
//		 ip = new Inputs();
//		 ip.Carrega("src/res/ecentos.csv");
//		 jogador.gravity = ip.gravidade;
//		 jogador.impoulso0 = ip.impulso0;
//		 jogador.impulso1 = ip.impulso1;
		quad = new Quadrado(tilePlat, 350, 470, 50, 50, 5);
		
	}
	public void geracaoPlat(){
		//int dburaco = rand.nextInt(500);
		for(int i =0; i< platList.size(); i++){
			if(platList.get(i).X+(platList.get(i).numQuad*16) < 800){
				//System.out.print(platList.get(0).numQuad);
				newPlat = true;
			}
			else{
				newPlat = false;
			}
		}
		if(newPlat){
			Plataforma plat = new Plataforma(null, 800+rand.nextInt(50)+100, 520-rand.nextInt(100), 800, 50,rand.nextInt(15)+10);
			platList.add(plat);
			one = false;
			newPlat=false;
		}
		
//		for(int i=0; i< platList.size(); i++){
//			if(platList.get(i).X+platList.get(i).TX >0){
//				platList.remove(i);
//			}
//		}
		
	}

	@Override
	public void Simulase(int difTime) {
		
		if(jogador.VelY> MAX_FALL_SPEED){
			jogador.VelY = MAX_FALL_SPEED;
		}
			
//		if(LEFT){
//			jogador.VelX = -vel;
//		}else if(RIGHT){
//			jogador.VelX = vel;
//		}else{
//			jogador.VelX = 0;
//		}
		jogador.Simulase(difTime);
		
		for(int i =0; i< platList.size(); i++){
			platList.get(i).Simulase(difTime);

		}
		geracaoPlat();
		
		quad.Simulase(difTime);
	}

	@Override
	public void Desenha(Graphics2D dbg) {
		dbg.setColor(Color.green);
		DesenhaFundo(dbg, fundo);
		
		jogador.Desenha(dbg);
		for(int i =0; i< platList.size(); i++){
		
			platList.get(i).Desenha(dbg);

		}
		quad.Desenha(dbg);
		
	}
	float X1 = 0;
	float X2 = X1 + 1000;
	
	public void DesenhaFundo(Graphics2D dbg, BufferedImage BackGround){
		
		
		
		if((X1 +BackGround.getWidth()) < 0){
			X1 = X2 + BackGround.getWidth();
		}else if((X2 +BackGround.getWidth()) < 0){
			X2 = X1 + BackGround.getWidth();
		}
		X1 += -100*GamePanel.instance.diffTime/1000.0;
		X2 += -100*GamePanel.instance.diffTime/1000.0;
		//System.out.println(X1);
		//System.out.println(X2);
		
		dbg.drawImage(BackGround, (int)X1, 0, BackGround.getWidth(), GamePanel.PHEIGHT, null);
		dbg.drawImage(BackGround, (int)X2, 0, BackGround.getWidth(), GamePanel.PHEIGHT, null);
		
		
	}

	@Override
	public void keyPressed(KeyEvent e) {
		int key = e.getKeyCode();
		if(key == KeyEvent.VK_UP){
			
			
		}
		if(key == KeyEvent.VK_DOWN){
			//DOWN = true;
			
		}
		if(key == KeyEvent.VK_LEFT){
			LEFT = true;
			
		}
		if(key == KeyEvent.VK_RIGHT){
			RIGHT = true;
			
		}
		if(key == KeyEvent.VK_R){
			
			platList.clear();
			GamePanel.instance.telaAtiva = new Tela01();
			
		}
		
		
	}

	@Override
	public void keyReleased(KeyEvent e) {
		int key = e.getKeyCode();
		if(key == KeyEvent.VK_DOWN){
			//DOWN = false;
			
			
		}
		if(key == KeyEvent.VK_UP){
		
			
		}                                                
		if(key == KeyEvent.VK_LEFT){                                                                          
			LEFT = false;
			
		}
		if(key == KeyEvent.VK_RIGHT){
			RIGHT = false;
			
		}
		if(key == KeyEvent.VK_SPACE){                                                                                              
			JUMP = false;
			
		}
	}

	@Override
	public void MouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseDragged(MouseEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		jogador.EndJump();
		JUMP = false;
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		jogador.StartJump();
		JUMP = true;
		
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseWheelMoved(MouseWheelEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
